﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.Domain;
using System.Web.Mvc;
using System.Collections;
using System.ComponentModel;
 

namespace MShop.Web.Components.Extensions
{
    public  static class EnumExtension
    {
        public static IList<System.Web.Mvc.SelectListItem> ConvertListItem(this Type enumType,object selectedValue=null,SelectListItem zeroListItem=null)
        {
            IList<System.Web.Mvc.SelectListItem> list = new List<System.Web.Mvc.SelectListItem>();

            Enum.GetNames(enumType).ToList().ForEach((x) =>
            {
                SelectListItem item = null;
                object obj = Enum.Parse(enumType, x);
                RightsDescriptionAttribute attr = obj.GetType().GetField(x).GetCustomAttributes(typeof(RightsDescriptionAttribute), false).OfType<RightsDescriptionAttribute>().FirstOrDefault();
                DescriptionAttribute descAttr = obj.GetType().GetField(x).GetCustomAttributes(typeof(DescriptionAttribute), false).OfType<DescriptionAttribute>().FirstOrDefault();
                if (attr != null)
                {
                    if (attr.Display)
                    {
                        item = new System.Web.Mvc.SelectListItem() { Text = attr.Description, Value = ((int)Enum.Parse(enumType, x)).ToString() };
                    }
                }
                else if (descAttr != null)
                {
                    if (!string.IsNullOrEmpty(descAttr.Description))
                    {
                        item = new System.Web.Mvc.SelectListItem() { Text = descAttr.Description, Value = ((int)Enum.Parse(enumType, x)).ToString() };
                    }
                    else
                    {
                        item = new System.Web.Mvc.SelectListItem() { Text = x, Value = ((int)Enum.Parse(enumType, x)).ToString() };
                    }
                }
                else
                {
                    item = new System.Web.Mvc.SelectListItem() { Text = x, Value = ((int)Enum.Parse(enumType, x)).ToString() };
                }
                if (item != null)
                {
                    if (selectedValue != null)
                    {
                        if (selectedValue.GetType().IsArray)
                        {
                            IEnumerable array = (IEnumerable)selectedValue;
                            foreach (var eitem in array)
                            {
                                if (eitem.ToString() == item.Value)
                                    item.Selected = true;
                            }
                            
                        }
                        else
                        {
                            if (selectedValue.ToString() == item.Value)
                                item.Selected = true;
                        }

                    }
                    list.Add(item);
                }
            });
            if (zeroListItem != null)
            {
                list.Insert(0, zeroListItem);
            }
            return list;
        }
    }
}